$.fn.exchangeGoods = function(opts){
	var self = this;
	if (self.length<1 || !opts || !opts.getParam) return self;
	
	var html = '<p>确定兑换此商品？ <label class="ui-button ui-button-impt JS_ExchangeBtn">'+
						'<button class="ui-button-inner">兑换</button>'+
					'</label> <label class="ui-button JS_Cancel">'+
						'<button class="ui-button-inner">取消</button>'+
					'</label></p>',
		getParam;
	
	getParam = opts.getParam;
	onSuccess = opts.onSuccess;
	
	self.each(function(){
		var item = $(this), confirmBox;
		
		item.click(function(){
			createConfirmBox(item);
			confirmBox.open();
			return false;
		});
		
		function createConfirmBox(target){
			if (!confirmBox){
				var content = $(html), running;
				confirmBox = new $.DialogBox(target, content);
				content.find('.JS_Cancel').click(function(){
					confirmBox.close();
				});
				content.find('.JS_ExchangeBtn').click(function(){
					if (running) return false;
					running = true;
					/*
					var count = $('#JS_Quantity').val(),
						id = $('#JS_DiscountId').val();
					*/
					var param = getParam.call(target);
					$.Goods.exchange(param.id, param.count, {
						success: function(msg){
							$.DialogMsg.show(msg, 1);
							confirmBox.close();
							if ($.isFunction(onSuccess)) onSuccess.call(target, param.count);
						},
						fail: function(msg){
							confirmBox.close();
							var box = createFeedback(target, msg);
							box.open();
						},
						complete: function(){
							running = false;
						}
					});
				});
			}
		}
		
		function createFeedback(target, msg){
			var content = $('<p>'+msg+'</p>'),
				box = new $.DialogBox(target, content);
			
			content.append($('<label class="ui-button">'+
							'<button class="ui-button-inner">知道了</button>'+
						'</label>').click(function(){
							box.close();
						}));
			return box;
		}
	});
	
};